package com.probiz.estore.content.service.impl;

import java.util.List;

import org.hibernate.criterion.Restrictions;

import com.probiz.estore.common.model.content.SystemMessage;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.content.service.SystemMessageManager;
import com.probiz.estore.content.dao.SystemMessageDao;


/**
 * Manager implementation for SystemMessage, responsible for business processing, and communicate between web and persistence layer.
 */
public class SystemMessageManagerImpl extends GenericManagerImpl<SystemMessage> implements SystemMessageManager {

	private SystemMessageDao systemMessageDao = null;

	/**
	 * @param systemMessageDao
	 *            the systemMessageDao to set
	 */
	public void setSystemMessageDao(SystemMessageDao systemMessageDao) {
		this.systemMessageDao = systemMessageDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = systemMessageDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(SystemMessage entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(SystemMessage entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.service.SystemMessageManager#addSystemMessage(java.lang.String, java.lang.Short, java.lang.Integer)
	 */
	public void addSystemMessage(String messageHtml,Short type,Integer appUserId){
		SystemMessage systemMessage=new SystemMessage();
	    systemMessage.setAppuserId(appUserId);	    
	    systemMessage.setMessageHtml(messageHtml);
	    systemMessage.setStatus((short)SystemMessage.MESSAGE_NEVER_READ);
	    systemMessageDao.save(systemMessage);
	}

	public Integer[] getCountsOfCustomer(Integer currentUserId) {
		return systemMessageDao.getCountsOfCustomer(currentUserId);
	}

	public void readMessage(Integer messageId) {
		systemMessageDao.readMessage(messageId);
	}

	public Long countNeverReadMessage(Integer appUserId) {
		return systemMessageDao.countNeverReadMessage(appUserId);//countByProperty("status", SystemMessage.MESSAGE_NEVER_READ);
	}

	public Long countAllMessage(Integer appUserId) {
		return systemMessageDao.countByProperty("appUser.appuserId", appUserId); 
	}

	public List<SystemMessage> readNeverReadMessage(Integer currentUserId) {
		return systemMessageDao.readNeverReadMessage(currentUserId);
	}

}
